program BuildDll;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows,
  Classes,
  ActiveX;

var
  LIB_GUID, I_GUID, CLASS_GUID: string;
  AppPath: string;
  tmpGUID: TGUID;
  slIDL, slTlbPas, slDpr, slImpPas: TStringList;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  AppPath := ExtractFilePath(ParamStr(0));
  if CoCreateGUID(tmpGUID)=S_OK then
  begin
    LIB_GUID := GUIDToString(tmpGUID);
  end else
  begin
    WriteLn('Create LIB_GUID error!');
    ExitProcess(1);
  end;
  if CoCreateGUID(tmpGUID)=S_OK then
  begin
    I_GUID := GUIDToString(tmpGUID);
  end else
  begin
    WriteLn('Create I_GUID error!');
    ExitProcess(1);
  end;
  if CoCreateGUID(tmpGUID)=S_OK then
  begin
    CLASS_GUID := GUIDToString(tmpGUID);
  end else
  begin
    WriteLn('Create CLASS_GUID error!');
    ExitProcess(1);
  end;

  slIDL := TStringList.Create;
  slTlbPas := TStringList.Create;
  slDpr := TStringList.Create;
  slImpPas := TStringList.Create;
  try
    slIDL.LoadFromFile(AppPath + 'template\InvokeGame.idl');
    slTlbPas.LoadFromFile(AppPath + 'template\InvokeGame_TLB.pas');
    slDpr.LoadFromFile(AppPath + 'template\InvokeGame.dpr');
    slImpPas.LoadFromFile(AppPath + 'template\uInvokeGame.pas');    

    LIB_GUID[1] := '(';
    LIB_GUID[Length(LIB_GUID)] := ')';
    I_GUID[1] := '(';
    I_GUID[Length(I_GUID)] := ')';
    CLASS_GUID[1] := '(';
    CLASS_GUID[Length(CLASS_GUID)] := ')';
    slIDL.Text := StringReplace(slIDL.Text,
                  '(200D26D8-B19B-4323-B98E-291C976BD209)',
                  LIB_GUID,
                  [rfReplaceAll]);
    slIDL.Text := StringReplace(slIDL.Text,
                  '(AD3CA404-F869-4262-8EF4-DE447B34E43F)',
                  I_GUID,
                  [rfReplaceAll]);
    slIDL.Text := StringReplace(slIDL.Text,
                  '(1F313FF1-9869-49FA-A31A-78AD032629B9)',
                  CLASS_GUID,
                  [rfReplaceAll]);
    LIB_GUID[1] := '{';
    LIB_GUID[Length(LIB_GUID)] := '}';
    I_GUID[1] := '{';
    I_GUID[Length(I_GUID)] := '}';
    CLASS_GUID[1] := '{';
    CLASS_GUID[Length(CLASS_GUID)] := '}';                  


    slTlbPas.Text := StringReplace(slTlbPas.Text,
                  '{200D26D8-B19B-4323-B98E-291C976BD209}',
                  LIB_GUID,
                  [rfReplaceAll]);
    slTlbPas.Text := StringReplace(slTlbPas.Text,
                  '{AD3CA404-F869-4262-8EF4-DE447B34E43F}',
                  I_GUID,
                  [rfReplaceAll]);
    slTlbPas.Text := StringReplace(slTlbPas.Text,
                  '{1F313FF1-9869-49FA-A31A-78AD032629B9}',
                  CLASS_GUID,
                  [rfReplaceAll]);

    if ParamCount>0 then
    begin
      slTlbPas.Text := StringReplace(slTlbPas.Text,
                    'unit InvokeGame_TLB;',
                    Format('unit %s_TLB;', [ParamStr(1)]),
                    [rfReplaceAll]);
      slIDL.Text := StringReplace(slIDL.Text,
                    'library InvokeGame',
                    'library ' + ParamStr(1),
                    [rfReplaceAll]);
      slDpr.Text := StringReplace(slDpr.Text,
                    'InvokeGame_TLB in ''InvokeGame_TLB.pas''',
                    Format('%s_TLB in ''%s_TLB.pas''', [ParamStr(1), ParamStr(1)]),
                    [rfReplaceAll]);
      slImpPas.Text := StringReplace(slImpPas.Text,
                    'InvokeGame_TLB',
                    ParamStr(1) + '_TLB',
                    [rfReplaceAll]);                                
    end;
    if ParamCount>1 then
    begin
      slIDL.Text := StringReplace(slIDL.Text,
                    'coclass InvokeGame',
                    'coclass ' + ParamStr(2),
                    [rfReplaceAll]);
    end;
                                    
    DeleteFile(PChar(AppPath + 'src\InvokeGame.idl'));
    slIDL.SaveToFile(AppPath + 'src\InvokeGame.idl');
    if ParamCount>0 then
    begin
      DeleteFile(PChar(AppPath + Format('src\%s_TLB.pas', [ParamStr(1)])));
      slTlbPas.SaveToFile(AppPath + Format('src\%s_TLB.pas', [ParamStr(1)]));
    end else
    begin
      DeleteFile(PChar(AppPath + 'src\InvokeGame_TLB.pas'));
      slTlbPas.SaveToFile(AppPath + 'src\InvokeGame_TLB.pas');
    end;
    DeleteFile(PChar(AppPath + 'src\InvokeGame.dpr'));
    slDpr.SaveToFile(AppPath + 'src\InvokeGame.dpr');
    DeleteFile(PChar(AppPath + 'src\uInvokeGame.pas'));
    slImpPas.SaveToFile(AppPath + 'src\uInvokeGame.pas');
  finally
    slImpPas.Free;
    slDpr.Free;
    slTlbPas.Free;
    slIDL.Free;
  end;
end.
